package com.zhouheng.springbootmultithreading.module.bankaccount.controller;

import com.zhouheng.springbootmultithreading.module.bankaccount.entity.BankAccountVo;
import com.zhouheng.springbootmultithreading.common.exception.BaseException;
import com.zhouheng.springbootmultithreading.module.bankaccount.service.BankAccountService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 银行账户，存取款
 *
 * @author 周恒
 * @date 20190117 17:28:57
 * @since v1.0
 */
@RestController
@RequestMapping("/account")
public class BankAccountController {

    @Autowired
    private BankAccountService bankAccountService;

    /**
     * 查看余额
     *
     * @author 周恒
     * @date 20190117 17:28:58
     * @since v1.0
     */
    @PostMapping("/find")
    public Double findMoney(@RequestBody BankAccountVo vo){
        if(null == vo || StringUtils.isAnyBlank(vo.getAccount(),vo.getPassword())){
            throw new BaseException("账号或密码为空");
        }
        return bankAccountService.findMoney(vo);
    }

    /**
     * 取钱，并返回余额
     *
     * @author 周恒
     * @date 20190117 17:28:58
     * @since v1.0
     */
    @PostMapping("withdraw")
    public Double withdrawMoney(@RequestBody BankAccountVo vo){
        if(null == vo || StringUtils.isAnyBlank(vo.getAccount(),vo.getPassword())){
            throw new BaseException("账号或密码不能为空");
        }
        if(vo.getMoney() <= 0D){
            throw new BaseException("取款金额错误");
        }
        return bankAccountService.withdrawMoney(vo);
    }

    /**
     * 存钱，并返回余额
     *
     * @author 周恒
     * @date 20190117 17:28:58
     * @since v1.0
     */
    @PostMapping("save")
    public Double saveMoney(@RequestBody BankAccountVo vo){
        if(null == vo || StringUtils.isAnyBlank(vo.getAccount(),vo.getPassword())){
            throw new BaseException("账号或密码不能为空");
        }
        if(vo.getMoney() <= 0D){
            throw new BaseException("存款金额错误");
        }
        return bankAccountService.saveMoney(vo);
    }

}
